<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" ref="searchForm">
            <el-form-item prop="msgType" label="消息类型:">
              <el-select v-model="searchForm.msgType" :clearable="true" placeholder="请选择消息类型">
                <el-option v-for="item in msgType" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="msgStatus" label="处理状态:">
              <el-select v-model="searchForm.disposeState" :clearable="true" placeholder="请选择消息处理状态">
                <el-option v-for="item in msgDisposeState" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
            <el-button size="mini" v-has="permissionOneself" type="success" icon="el-icon-user-solid" plain @click="onSelfDisposeMesssageSearch">自己处理的消息</el-button>
            <el-button size="mini" v-has="permissionAwait" type="danger" icon="el-icon-warning" @click="onAwaitDisposeMessageSearch">待处理的消息</el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table ref="tableList" :data="tableList" :fit="true">
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column prop="remindType" label="消息类型" width="200">
          <template slot-scope="scope">
            <span v-if="scope.row.remindType === 1"> 万能键 </span>
            <span v-if="scope.row.remindType === 2"> 设备离线异常 </span>
            <span v-if="scope.row.remindType === 3"> 设备报警异常 </span>
          </template>
        </el-table-column>
        <el-table-column prop="remindContent" label="消息提醒内容" align="center"></el-table-column>
        <el-table-column prop="disposeResult" label="处理结果" align="center"></el-table-column>
        <el-table-column label="处理状态" width="120" align="center">
          <template slot-scope="scope">
            <el-tag :type="formatDsiposeStates(scope.row.disposeState)" effect="dark" size="mini">
              <span>{{ formatDsiposeState(scope.row.disposeState) }}</span>
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="disposeUserName" label="处理人" align="center"></el-table-column>
        <el-table-column prop="createDate" label="创建时间" align="center"></el-table-column>
        <el-table-column prop="disposeBeginDate" label="开始处理时间" align="center"></el-table-column>
        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-row>
              <el-button v-has="permissionEdit" v-if="scope.row.disposeState === 1 || scope.row.disposeState === 5" @click="disposeMessage(scope.row)" size="mini">前往处理</el-button>
              <el-button v-has="permissionCancel" v-if="scope.row.disposeState === 1" @click="cancelDisposeMessage(scope.row)" size="mini">取消</el-button>
              <el-button v-has="permissionCancel" v-if="scope.row.disposeState === 5" @click="cancelDisposeMessage(scope.row)" size="mini">重置</el-button>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import ServerMessageApi from '@/api/message/serverMessage';
import { mapActions } from 'vuex';

export default {
  name: 'ServerMessageManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      loadingIns: false,
      searchForm: {
        msgType: '', //消息类型
        disposeState: 1, //处理状态
        // oneself:0,
      },
      listType: 0,
      tableList: [],
      selectIds: [],

      page: {
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
      },
      msgType: [
        { label: '万能键', value: 1 },
        { label: '设备离线异常', value: 2 },
        { label: '设备报警异常', value: 3 },
      ],
      msgDisposeState: [
        { label: '待处理', value: 1 },
        { label: '处理中', value: 5 },
        { label: '已取消', value: 15 },
        { label: '已处理', value: 20 },
      ],
      permissionOneself: 'SYSTEM_MANAGER_MESSAGE_SERVERMESSAGE_LIST_ONESELF',
      permissionAwait: 'SYSTEM_MANAGER_MESSAGE_SERVERMESSAGE_LIST_AWAIT',
      permissionEdit: 'SYSTEM_MANAGER_MESSAGE_SERVERMESSAGE_LIST_EDIT',
      permissionCancel: 'SYSTEM_MANAGER_MESSAGE_SERVERMESSAGE_LIST_CANCEL',
    };
  },
  components: {
    // uploadFile
  },
  mounted() {
    this.getTableHeight();
    this.listTable();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listTable(this.listType);
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    // ...mapActions({
    //   setUnReadCount: 'setUnReadCount'
    // }),
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode === 13) {
        this.onSearch();
      }
    },

    listTable(listType = 0) {
      this.listType = listType;
      let data = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        remindType: '', //消息类型
        // disposeState:this.searchForm.disposeState, //处理状态
      };
      // listType为0表示普通的查询，为1表示查看自己的消息列表，为2表示查看待处理的消息列表
      if (listType === 0) {
        data.remindType = this.searchForm.remindType;
        data.disposeState = this.searchForm.disposeState;
      } else if (listType === 1) {
        data.oneself = 1;
      } else if (listType === 2) {
        data.disposeState = 1;
      }
      ServerMessageApi.listServerRemind(data)
        .then((res) => {
          this.tableList = res.data.list;
          this.page.pageTotal = res.data.total;
          this.$refs.tableList.bodyWrapper.scrollTop = 0;
          this.searchForm.oneself = 0;
        })
        .catch((error) => {
          console.error(error);
        });
    },

    resetForm() {
      (this.searchForm.msgType = ''), (this.searchForm.disposeState = '');
      this.onSearch();
      // this.searchForm.oneself = 0
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listTable(0);
    },
    // 查看自己的处理消息列表
    onSelfDisposeMesssageSearch() {
      this.page.pageNo = 1;
      this.listTable(1);
    },
    // 待处理的消息列表
    onAwaitDisposeMessageSearch() {
      this.page.pageNo = 1;
      this.listTable(2);
    },
    // 前往处理消息
    disposeMessage(row) {
      ServerMessageApi.updateProcessing({ id: row.remindId, state: 1 }).then((res) => {
        this.$router.push({
          path: '/message/serverMessageEdit',
          query: {
            id: row.remindId,
          },
        });
      });
    },
    // 取消或者重置消息
    cancelDisposeMessage(row) {
      if (row.disposeState === 1) {
        this.$confirm('确定取消这条消息？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
        }).then((res) => {
          ServerMessageApi.cancelServerRemind({ id: row.remindId, state: 1 }).then((res) => {
            this.listTable(this.listType);
          });
        });
      } else if (row.disposeState === 5) {
        this.$confirm('确认重置消息状态？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
        }).then(() => {
          ServerMessageApi.resetMessage({ id: row.remindId, state: 1 }).then((res) => {
            this.listTable(this.listType);
          });
        });
      }
    },

    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listTable(this.listType);
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listTable(this.listType);
    },
    formatDsiposeState(cellValue) {
      if (cellValue === 1) {
        return '待处理';
      } else if (cellValue === 5) {
        return '处理中';
      } else if (cellValue === 15) {
        return '已取消';
      } else if (cellValue === 20) {
        return '已处理';
      }
    },
    formatDsiposeStates(cellValue) {
      if (cellValue === 1) {
        return 'danger';
      } else if (cellValue === 5) {
        return 'wraning';
      } else if (cellValue === 15) {
        return 'info';
      } else if (cellValue === 20) {
        return 'success';
      }
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
